import {apiList} from "./api_data.js"

class ApiView {
  constructor() {
  }

  initTable({name, desc, data = []}) {
    $(`<br><hr><h3>${name}</h3><p>${desc}</p>`).appendTo($('#apiContent'))
    let thList = ['接口名称', '接口路径', '说明'].map(item => `<th>${item}</th>`).join('')
    let mTable = $(`<table><thead><tr>${thList}</tr></thead></table>`).appendTo($('#apiContent'))
    let mTbody = $(`<tbody>${data.map(item => this.initTableContent(item)).join('')}</tbody>`).appendTo(mTable)
  }

  initTableContent(data) {
    let tdList = Object.keys(data).map(key => {
      if (key == 'url') return `<td><a href='${data[key]}'>${data[key]}</a></td>`
      else return `<td>${data[key]}</td>`
    })
    let trStr = `<tr>${tdList.join('')}</tr>`
    return trStr
  }

  addData() {
    let apiContent = apiList.map(apiItem => {
      this.initTable(apiItem)
    })
  }

}

window.onload = function () {
  let apiView = new ApiView()
  apiView.addData()
}
